package com.hl.vcd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hl.common.utils.OssUtils;
import com.hl.common.utils.VideoTypeUtils;
import com.hl.vcd.entity.Video;
import com.hl.vcd.entity.vo.VideoSearch;
import com.hl.vcd.mapper.VideoMapper;
import com.hl.vcd.service.VideoService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author 朱炫达
 */
@Service
@Transactional
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {
    @Override
    public boolean addVideo(Video video, MultipartFile imageFile, MultipartFile videoFile) {
        String videoUrl = null;
        String imageUrl = null;
        try {
            if (ObjectUtils.isEmpty(imageFile) || imageFile.getSize() <= 0 || ObjectUtils.isEmpty(videoFile) || videoFile.getSize() <= 0) {
                throw new Exception("上传文件大小为空");
            }
            imageUrl = OssUtils.uploadFile(imageFile,OssUtils.typeImage);
            videoUrl = OssUtils.uploadFile(videoFile,OssUtils.typeVideo);
            video.setCoverUrl(imageUrl).setVideoUrl(videoUrl);
            baseMapper.insert(video);
        }catch (Exception e){
            e.printStackTrace();
            if (imageUrl != null){
                OssUtils.deleteFile(imageUrl);
            }
            if (videoUrl != null){
                OssUtils.deleteFile(videoUrl);
            }
            return false;
        }
        return true;
    }

    @Override
    public List<Video> getTopVideoByLike(Integer number, Integer type) {
        return baseMapper.getTopVideoByLike(number,type);
    }

    @Override
    public List<Video> getTopVideoByPlay(Integer number) {
        return baseMapper.getTopVideoByPlay(number);
    }

    @Override
    @Cacheable(value = "getVideoById",key = "#id",sync = true)
    public Video getVideoById(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void updateVideoById(Video video) {
        baseMapper.updateById(video);
    }

    @Override
    public void collectionIncrement(String id) {
        UpdateWrapper<Video> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.setSql("collection_number = collection_number + 1");
        this.update(wrapper);
    }

    @Override
    public void collectionDecrement(String id) {
        UpdateWrapper<Video> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.setSql("collection_number = collection_number - 1");
        this.update(wrapper);
    }

    @Override
    public void likeIncrement(String id) {
        UpdateWrapper<Video> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.setSql("like_number = like_number + 1");
        this.update(wrapper);
    }

    @Override
    public void likeDecrement(String id) {
        UpdateWrapper<Video> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.setSql("like_number = like_number - 1");
        this.update(wrapper);
    }

    @Override
    public IPage<VideoSearch> searchVideo(Page<VideoSearch> page, String keyword, String type, String order) {
        QueryWrapper<VideoSearch> wrapper = new QueryWrapper<>();
        wrapper.like(keyword!=null,"video.video_title",keyword);
        if (type != null){
            switch (type) {
                case "1":
                    wrapper.eq("video.type", VideoTypeUtils.ANIME);
                    break;
                case "2":
                    wrapper.eq("video.type", VideoTypeUtils.ENTERTAINMENT);
                    break;
                case "3":
                    wrapper.eq("video.type", VideoTypeUtils.SCIENCE_AND_TECHNOLOGY);
                    break;
            }
        }
        if (order != null){
            switch (order) {
                case "0":
                    wrapper.orderByDesc("video.play_number");
                    break;
                case "2":
                    wrapper.orderByDesc("video.like_number");
                    break;
                case "1":
                    wrapper.orderByDesc("video.gmt_create");
                    break;
            }
        }
        return baseMapper.searchVideo(page,wrapper);
    }

}
